<?php

namespace App\Admin\Controllers;

use App\Models\Page;
use App\Models\Template;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Helper\Build\Build;
use App\Models\Area;


class PageController extends Controller
{
    use ModelForm;

    protected $name = '单页管理';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('列表');

            $content->body(Page::tree(function ($tree) {

                $tree->tools(function($tool){
                    $tool->add('<a class="btn btn-info btn-sm" onclick="build_page(0)">生成所有</a>');
                });
                
            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');


            $content->body($this->form());
        });
    }
	
	public function build_page()
	{
		$Build = new Build();
		$id = request()->get('id', 0);
        if($id == 0)
        {
            $variable = Page::all();
            foreach ($variable as $key => $value) {
                $Build->page($value);
            }
        }
        return ['status'=>1, 'message'=> 'OK'];
		
        
		
	}

    

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Page::class, function (Form $form) {

			$form->number('id', 'ID')->attribute('disabled', false);
            $form->text('name', '分类名称')->rules('required|min:3');
            $form->text('description', '分类描述');
            $form->number('listorder', '排序');
            $form->select('parent_id', '父栏目')->options(Page::selectOptions())->rules('required');
            $form->select('temp_id', '模板')->options(Template::selectOptions(3));
            
            $form->text('seo_title', 'SEO标题');
            $form->text('seo_keywords', 'SEO关键词');
            $form->text('seo_description', 'SEO描述');
            //$form->editor('contentccc', '描述');//->rules('required')
            $form->editor('content', '内容')->help('如果是单页内容则填写，比如关于我们，联系我们');
			
			$form->saved(function (Form $form){
				$Build = new Build();
				$Build->page($form->model());
			});
        });
    }
}
